package com.molin.fileter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(value = {"/addStudentInfo.jsp","/listStudent.jsp"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1.将请求和响应转换为Http协议相关
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;

        //2.获取会话域对象中的数据
        Object username = request.getSession().getAttribute("username");
        //3.判断用户名
        if(username==null||"".equals(username)){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
        }

        //4.如果验证没问题的话，放行
        filterChain.doFilter(request,response);
    }
}
